package dnl.toolbox.time;

import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.joda.time.DateTimeZone;
import org.joda.time.Interval;
import org.joda.time.MutableDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;


/**
 * 
 * @author Daniel Orr
 */
public class TimeUtils {

	private static DateTime fakeTime = null;

	private static DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy");
	private static DateTimeFormatter fmtUTC = DateTimeFormat.forPattern("dd.MM.yyyy").withZone(DateTimeZone.UTC);

	public static Interval parseInterval(String intervalStr){
		String[] splits = StringUtils.split(intervalStr, "-");
		DateTime date1 = parseDate(splits[0]);
		DateTime date2 = parseDate(splits[1]);
		return new Interval(date1, date2);
	}

	public static Interval parseIntervalUTC(String intervalStr){
		String[] splits = StringUtils.split(intervalStr, "-");
		DateTime date1 = parseDateUTC(splits[0]);
		DateTime date2 = parseDateUTC(splits[1]);
		return new Interval(date1, date2);
	}

	public static DateTime parseDate(String dateStr){
		return fmt.parseDateTime(dateStr);
	}

	public static DateTime parseDateUTC(String dateStr){
		return fmtUTC.parseDateTime(dateStr);
	}
	
	public static String formatDate(DateTime time){
		return fmt.print(time);
	}

	public static String formatDateUTC(DateTime time){
		return fmtUTC.print(time);
	}
	
	public static void setFakeTime(DateTime time) {
		fakeTime = time;
		DateTimeUtils.setCurrentMillisFixed(time.getMillis());
	}

	public static void setFakeTime(String timeStr) {
		fakeTime = fmt.parseDateTime(timeStr);
	}

	public static void unsetFakeTime() {
		fakeTime = null;
	}

	public static DateTime getCurrentTime() {
		if (fakeTime != null)
			return fakeTime;
		return new DateTime();
	}

	public static DateTime getBeginingOfDay(DateTime dt){
		MutableDateTime mdt = new MutableDateTime(dt);
		mdt.setMillisOfDay(0);
		return new DateTime(mdt);
	}
	
	public static DateTime getLastMillisOfDay(DateTime dt){
		MutableDateTime mdt = new MutableDateTime(dt);
		mdt.setMillisOfDay(1000*60*60*24-1);
		return new DateTime(mdt);
	}

	public static DateTime getBeginingOfWeek(DateTime dt){
		MutableDateTime mdt = new MutableDateTime(dt);
		mdt.setDayOfWeek(1);
		mdt.setMillisOfDay(0);
		return new DateTime(mdt);
	}

	public static DateTime getBeginingOfMonth(DateTime dt){
		MutableDateTime mdt = new MutableDateTime();
		mdt.setDayOfMonth(1);
		mdt.setMillisOfDay(0);
		mdt.setMonthOfYear(dt.getMonthOfYear());
		mdt.setYear(dt.getYear());
		return new DateTime(mdt);
	}
	
	public static DateTime getNextDay(DateTime dt){
		return dt.dayOfWeek().addToCopy(1);
	}
	
	/**
	 * 
	 * @param elapsedTime
	 * @return
	 */
	public static String formatElapsedTime(long elapsedTime) {
		String format = String.format("%%0%dd", 2);
		elapsedTime = elapsedTime / 1000;
		String seconds = String.format(format, elapsedTime % 60);
		String minutes = String.format(format, (elapsedTime % 3600) / 60);
		String hours = String.format(format, elapsedTime / 3600);
		String time = hours + ":" + minutes + ":" + seconds;
		return time;
	}
}
